package com.loe.ktui.custom

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.loe.ktui.*
import com.loe.ktui.util.KtColor
import com.loe.picker.PickerDialog
import com.loe.picker.util.PickerBean
import kotlinx.android.synthetic.main.kt_view_form_switch.view.*
import org.json.JSONArray
import org.json.JSONObject

//open class UiFormSwitchView(id: String? = null) : UiFrameLayout(id)
//{
//    override var width: Any? = MATCH_PARENT
//
//    var isInfo: Boolean? = null
//    var title: CharSequence? = null
//    var notNull: Boolean? = null
//    var text: CharSequence? = null
//    var unit: CharSequence? = null
//    var hint: CharSequence? = null
//    var formGravity: Int? = null
//    var textSize: Any? = 15
//    var titleSize: Any? = null
//    var textColor: Int? = null
//    var titleColor: Int? = null
//    var unitColor: Int? = null
//    var titleLeft: Any? = null
//    var titleLen: Any? = null
//    var tailLen: Any? = null
//
//    var onSelect: (KtFormSwitchView.(i: Int, bean: PickerBean) -> Unit)? = null
//    fun onSelect(onSelect: KtFormSwitchView.(i: Int, bean: PickerBean) -> Unit)
//    {
//        this.onSelect = onSelect
//    }
//
//    fun initUI(context: Context, view: KtFormSwitchView)
//    {
//        super.initUI(context, view)
//
//        isInfo?.let { view.isInfo = it }
//        title?.let { view.title = it }
//        notNull?.let { view.notNull = it }
//        text?.let { view.text = it }
//        unit?.let { view.unit = it }
//        view.hint = hint ?: (if (isInfo == true) "" else "请选择")
//        formGravity?.let { view.formGravity = it }
//        textSize?.let { view.setTextSizePx(it.distFloat) }
//        titleSize?.let { view.setTitleSizePx(it.distFloat) }
//        textColor?.let { view.textColor = it }
//        titleColor?.let { view.titleColor = it }
//        unitColor?.let { view.unitColor = it }
//        titleLeft?.let { view.titleLeft = it.distFloat }
//        titleLen?.let { view.titleLen = it.distFloat }
//        tailLen?.let { view.tailLen = it.distFloat }
//
//        onSelect?.let { onSelect -> view.setOnSelectListener { i, bean -> view.onSelect(i, bean) } }
//    }
//
//    override fun createView(context: Context, parent: ViewGroup?): KtFormSwitchView
//    {
//        return KtFormSwitchView(context).also()
//        {
//            initUI(context, it)
//            addToParent(parent, it)
//        }
//    }
//}
//
//fun Any.ui_FormSwitchView(id: String? = null, block: (UiFormSwitchView.() -> Unit)? = null): UiFormSwitchView
//{
//    val ui = UiFormSwitchView(id)
//    block?.let { ui.it() }
//    if (this is UiGroup) addChild(ui)
//    return ui
//}

/** 表单选择View */
open class KtFormSwitchView : KtFrameLayout
{
    constructor(context: Context) : super(context)
    {
        onCreate(null)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    {
        onCreate(attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    {
        onCreate(attrs)
    }

    private fun onCreate(attrs: AttributeSet?)
    {
        val root = inflate(context, R.layout.kt_view_form_switch, null) as ViewGroup?
        addView(root, MATCH_PARENT, MATCH_PARENT)

        attrs?.let()
        {
            val typedArray = context.obtainStyledAttributes(it, R.styleable.KtFormSwitchView)

            // isInfo
            isInfo = typedArray.getBoolean(R.styleable.KtFormSwitchView_kt_form_isInfo, false)
            // title
            title = typedArray.getString(R.styleable.KtFormSwitchView_kt_form_title) ?: ""
            // isChecked
            if(typedArray.hasValue(R.styleable.KtFormSwitchView_kt_form_isChecked))
            {
                switchView.setCheckedNoAnimate(typedArray.getBoolean(R.styleable.KtFormSwitchView_kt_form_isChecked, false))
            }
            // mainColor
            if(typedArray.hasValue(R.styleable.KtFormSwitchView_kt_form_checkMainColor))
            {
                mainColor = typedArray.getColor(R.styleable.KtFormSwitchView_kt_form_checkMainColor, mainColor)
            }
            // baseColor
            if(typedArray.hasValue(R.styleable.KtFormSwitchView_kt_form_checkBaseColor))
            {
                baseColor = typedArray.getColor(R.styleable.KtFormSwitchView_kt_form_checkBaseColor, baseColor)
            }

            // formGravity
            formGravity = typedArray.getInt(R.styleable.KtFormSwitchView_kt_form_gravity, Gravity.LEFT)
            // size
            textSize = dp(typedArray.getDimension(R.styleable.KtFormSwitchView_kt_form_textSize, ktPx(15).toFloat()))
            titleSize = dp(typedArray.getDimension(R.styleable.KtFormSwitchView_kt_form_titleSize, ktPx(15).toFloat()))
            // title_color
            titleColor = typedArray.getColor(R.styleable.KtFormSwitchView_kt_form_titleColor, KtColor.grayDeep)
            // text_color
            textColor = typedArray.getColor(R.styleable.KtFormSwitchView_kt_form_textColor, KtColor.text)
            // titleLeft
            titleLeft = typedArray.getDimension(R.styleable.KtFormSwitchView_kt_form_titleLeft, ktPx(15).toFloat())
            // titleLen
            titleLen = typedArray.getDimension(R.styleable.KtFormSwitchView_kt_form_titleLen, ktPx(110).toFloat())
            // tailLen
            tailLen = typedArray.getDimension(R.styleable.KtFormSwitchView_kt_form_tailLen, ktPx(15).toFloat())

            typedArray.recycle()
        }

        viewRoot.setOnClickListener { switchView.setChecked(!switchView.isChecked) }

        if (minimumHeight > 0)
        {
            root?.minimumHeight = minimumHeight
        }
    }

    var isInfo: Boolean = false
        set(value)
        {
            field = value
            switchView.isVisible = !value
            textView.isVisible = value
            textView.text = if (switchView.isChecked) "开" else "关"
        }

    var title: CharSequence
        get() = textTitle.text
        set(value)
        {
            textTitle.text = value
        }

    var isChecked: Boolean = false
        get() = switchView.isChecked
        set(value)
        {
            field = value
            switchView.setChecked(value)
        }

    var formGravity = Gravity.RIGHT
        set(value)
        {
            field = value
            textView.gravity = value
        }

    var titleSize = 0f
        set(value)
        {
            field = value
            textTitle.textSize = titleSize
        }

    var textSize = 0f
        set(value)
        {
            field = value
            textTitle.textSize = textSize
            textView.textSize = textSize
        }

    var titleLeft = 0f
        set(value)
        {
            field = value
            textTitle.marginLeftPx = value.toInt()
        }

    var titleLen = 0f
        set(value)
        {
            field = value
            viewContent.setPadding(value.toInt(), 0, tailLen.toInt(), 0)
        }

    var tailLen = 0f
        set(value)
        {
            field = value
            viewContent.setPadding(titleLen.toInt(), 0, value.toInt(), 0)
        }

    var titleColor = KtColor.grayDeep
        set(value)
        {
            field = value
            textTitle.setTextColor(value)
        }

    var textColor = KtColor.text
        set(value)
        {
            field = value
            textView.setTextColor(value)
        }

    var mainColor = KtColor.green
        set(value)
        {
            field = value
            switchView.mainColor = value
        }

    var baseColor = KtColor.white
        set(value)
        {
            field = value
            switchView.baseColor = value
        }

    fun setTitleSizePx(px: Float)
    {
        textTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, px)
    }

    fun setTextSizePx(px: Float)
    {
        textTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, px)
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, px)
    }

    private fun dp(pxValue: Float): Float
    {
        val scale = resources.displayMetrics.density
        return (pxValue + 0.5f) / scale
    }

    fun setOnChecked(value: Boolean?, onChecked: (isChecked: Boolean) -> Unit)
    {
        switchView.setOnChecked(value, onChecked)
    }

    fun setOnChecked(onChecked: (isChecked: Boolean) -> Unit)
    {
        switchView.setOnChecked(onChecked)
    }
}